//给定一个二叉树的根节点 root ，和一个整数 targetSum ，求该二叉树里节点值之和等于 targetSum 的 路径 的数目。 
//
// 路径 不需要从根节点开始，也不需要在叶子节点结束，但是路径方向必须是向下的（只能从父节点到子节点）。 
//
// 
//
// 示例 1： 
//
// 
//
// 
//输入：root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
//输出：3
//解释：和等于 8 的路径有 3 条，如图所示。
// 
//
// 示例 2： 
//
// 
//输入：root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
//输出：3
// 
//
// 
//
// 提示: 
//
// 
// 二叉树的节点个数的范围是 [0,1000] 
// 
// -10⁹ <= Node.val <= 10⁹ 
// -1000 <= targetSum <= 1000 
// 
//
// Related Topics 树 深度优先搜索 二叉树 👍 1775 👎 0

package leetcode.editor.cn;
//java:路径总和 III
public class Q0437PathSumIii {
    public static void main(String[] args){
        Solution solution = new Q0437PathSumIii().new Solution();
//        TreeNode root = new TreeNode(10);
//        root.left = new TreeNode(5);
//        root.left.left = new TreeNode(3);
//        root.left.left.left = new TreeNode(3);
//        root.right = new TreeNode(-3);
//        root.left.right = new TreeNode(2);
//        root.left.right.right = new TreeNode(1);
//        root.left.left.right = new TreeNode(-2);
//        root.right.right = new TreeNode(11);


        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(4);
        root.right = new TreeNode(8);
        root.left.left = new TreeNode(11);
//        root.left.right = new TreeNode(2);
        root.right.left = new TreeNode(13);
        root.right.right = new TreeNode(4);
        root.left.left.left = new TreeNode(7);
        root.left.left.right = new TreeNode(2);
//        root.left.right.left = new TreeNode(1);
//        root.left.right.right = new TreeNode(1);
//        root.right.left.left = new TreeNode(5);
//        root.right.left.right = new TreeNode(1);
        root.right.right.left = new TreeNode(5);
        root.right.right.right = new TreeNode(1);
        solution.pathSum(root, 22);
        System.out.println(solution.count);
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int count = 0;
    public int outTargetSum;
    public int pathSum(TreeNode root, int targetSum) {
        outTargetSum = targetSum;
        dfs(root);
        return count;
    }

    public void dfs(TreeNode root) {
        if (root == null) return;
        // 不能直接dfs(root, outTargetSum)，会死循环
        recur(root, outTargetSum);  // 以当前节点为起始，和为targetSum
        // 再次遍历左节点和右节点
        if (root.left != null) dfs(root.left);
        if (root.right != null) dfs(root.right);
    }

    public void recur(TreeNode root, long target) {
        if (root == null) return;
        if (root.val == target) {
            count++;
        }
        if (root.left != null) recur(root.left, target - root.val);
        if (root.right != null) recur(root.right, target - root.val);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}